// import ROBO_AVATAR from '@/assets/image/avatar_robo.png';
import SpaceBot from '@/components/SpaceBot';
import { SpaceType } from '@/types/spaceType';
import { useLocation } from '@umijs/max';
import { parse } from 'querystring';
import React, { useEffect, useRef, useState } from 'react';
import CommonList from '../spaceData/com/CommonList';
import AdministrativeKnowledge from './com/AdministrativeKnowledge';
import Collection from './com/Collection';
import Favorite from './com/Favorite';
import GoodWordsAndSentences from './com/GoodWordsAndSentences';
import ImportantTell from './com/ImportantTell';
import NavBar from './com/NavBar';
import Reference from './com/Reference';
import './index.less';

const SpaceData: React.FC = () => {
  const [type, setType] = useState<SpaceType>(SpaceType.UNKOWN);
  const [title, setTitle] = useState('');
  const [expendBot, setExpendBot] = useState(false);
  const [showSearch, setShowSearch] = useState(false);
  const [hitQuery, setHitQuery] = useState<any>(null);
  const favoriteRef = useRef(null);
  const collectionRef = useRef(null);
  const AdministrativeKnowledgeRef = useRef(null);
  const GoodWordsAndSentencesRef = useRef(null);

  const navChange = (item: any) => {
    setType(item.type);
    setTitle(item.title);
    setShowSearch(false);
  };

  const hitClick = (data: any) => {
    console.log('点击命中', data);
    setHitQuery(data);
    setShowSearch(true);
  };

  const location = useLocation();
  const locationParam: any = parse(location.search.replace('?', ''));
  useEffect(() => {
    if (locationParam) {
      const type = locationParam.type as SpaceType;
      const title = locationParam.title;
      setType(type);
      setTitle(title);
    }
  }, []);

  //训练新增成功
  const createSuccess = (params: any) => {
    console.log('训练新增成功', params);
    const type: SpaceType = params.name as SpaceType;
    if (type === SpaceType.SPACETFAVORITE) {
      // 好文收藏空间刷新
      if (favoriteRef && favoriteRef.current) {
        favoriteRef.current.reloadData();
      }
    }
    if (type === SpaceType.SPACETCOLLECTION) {
      // 定期收集空间刷新
      if (collectionRef && collectionRef.current) {
        collectionRef.current.reloadData();
      }
    }
    if (type === SpaceType.SPACETRUTH) {
      // 行政局知识空间
      if (AdministrativeKnowledgeRef && AdministrativeKnowledgeRef.current) {
        AdministrativeKnowledgeRef.current.reloadData();
      }
    }
    if (type === SpaceType.SPACETSENTENCES) {
      // 好词好句空间
      if (GoodWordsAndSentencesRef && GoodWordsAndSentencesRef.current) {
        GoodWordsAndSentencesRef.current.reloadData();
      }
    }
  };

  return (
    <div className="space-data-warp">
      <NavBar type={type} onChange={navChange}></NavBar>
      {!showSearch && (
        <div className={`space-data-body ${expendBot ? '' : 'show-margin-right'}`}>
          {/* 行政局知识空间 */}
          {type === SpaceType.SPACETRUTH && (
            <AdministrativeKnowledge
              ref={AdministrativeKnowledgeRef}
              title={title}
            ></AdministrativeKnowledge>
          )}
          {/* 重要讲话空间 */}
          {type === SpaceType.SPACETSPEECH && <ImportantTell title={title}></ImportantTell>}
          {/* 公文模板空间 */}
          {type === SpaceType.SPACETSREFERENCE && <Reference title={title}></Reference>}
          {/* 好文收藏空间 */}
          {type === SpaceType.SPACETFAVORITE && (
            <Favorite ref={favoriteRef} title={title}></Favorite>
          )}
          {/* 好词好句空间 */}
          {type === SpaceType.SPACETSENTENCES && (
            <GoodWordsAndSentences
              ref={GoodWordsAndSentencesRef}
              title={title}
            ></GoodWordsAndSentences>
          )}
          {/* 定期收集空间 */}
          {type === SpaceType.SPACETCOLLECTION && (
            <Collection ref={collectionRef} title={title}></Collection>
          )}
        </div>
      )}
      {showSearch && (
        <CommonList
          query={hitQuery}
          expendBot={expendBot}
          close={() => setShowSearch(false)}
        ></CommonList>
      )}
      <div className="space-bot">
        <SpaceBot
          router="spaceData"
          setExpendBot={setExpendBot}
          hitClick={hitClick}
          createSuccess={createSuccess}
        ></SpaceBot>
      </div>
    </div>
  );
};
export default SpaceData;
